package com.example.yin.service.impl;

import com.example.yin.dao.CollectMapper;
import com.example.yin.dao.ConsumerMapper;
import com.example.yin.dao.ConsumerSongListMapper;
import com.example.yin.dao.SongMapper;
import com.example.yin.domain.Collect;
import com.example.yin.domain.Consumer;
import com.example.yin.domain.ConsumerSongList;
import com.example.yin.domain.Song;
import com.example.yin.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;
    @Autowired
    private ConsumerSongListMapper consumerSongListMapper;
    @Autowired
    private SongMapper songMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public boolean addUser(Consumer consumer){
        Integer state=1;
        consumer.setState(state);
        ConsumerSongList consumerSongList = new ConsumerSongList();
        consumerSongList.setIntroduction(consumer.getNickname()+"的默认歌单");
        consumerSongList.setPic(consumer.getAvator());
        consumerSongList.setTitle("默认歌单");
        return consumerMapper.addUser(consumer);
    }
    @Override
    public Integer userLogin(String username, String password){

        Consumer consumer1 = consumerMapper.userLogin(username,password);
        if(consumer1 == null || consumer1.getState() == null)
            return 0;
        else if(consumer1.getState().equals(1)){
            return 1;
        }else if(consumer1.getState().equals(0)) {
            return 2;
        }
        else return -1;
    }
    @Override
    public boolean userUpdatePassword(Integer id,String newPassword){
        Consumer consumer = consumerMapper.selectById(id);
        consumer.setPassword(newPassword);
        if(consumerMapper.updateById(consumer)>0) {
            return true;
        }else{
            return false;
        }
    }
    @Override
    public boolean userUpdateInfo(Consumer consumer){
        if(consumerMapper.updateById(consumer)>0) {
            return true;
        }
        else{
            return false;
        }
    }


    @Override
    public boolean userLogout(Integer id){
        return consumerMapper.userLogout(id);
    }


    @Override
    public List<Consumer> loginStatus(String username) {

        return consumerMapper.loginStatus(username);
    }

    @Override
    public boolean updateUserAvator(Consumer consumer) {

        return consumerMapper.updateUserAvator(consumer) >0 ?true:false;
    }

    @Override
    public List<Consumer> userOfId(Integer id) {

        return consumerMapper.userOfId(id);
    }

    @Override
    public List<ConsumerSongList> queryConsumerSongList(String str,Integer userId) {
        return consumerSongListMapper.queryConsumerSongList(str,userId);
    }

    @Override
    public List<Song> querySongInList(String str, Integer userId) {
        List<Collect> collects = collectMapper.allLists(userId);
        List<Song> songs = new ArrayList<>();
        for(int i =0;i<collects.size();i++){
            songs.add(songMapper.selectById(collects.get(i).getSongId()));
        }

        Pattern p = Pattern.compile(str);
        List<Song> querySongs = new ArrayList<>();
        for(int i = 0;i<songs.size();i++){
            Matcher m = p.matcher(songs.get(i).getName());
            boolean result = m.find();
            if(result==true)querySongs.add(songs.get(i));
        }
        return querySongs;
    }



    /*admin module 管理员模块*/
    @Override
    public boolean existUser(String username) {
        return consumerMapper.existUsername(username)>0 ? true:false;
    }

    @Override
    public boolean veritypasswd(String username, String password) {

        return consumerMapper.verifyPassword(username, password)>0?true:false;
    }

    @Override
    public boolean updateUserMsg(Consumer consumer) {
        return consumerMapper.updateUserMsg(consumer)>0;
    }

    //    删除用户
    @Override
    public boolean deleteUser(Integer id) {
        return consumerMapper.deleteUser(id) >0 ?true:false;
    }
    //
    @Override
    public List<Consumer> allUser() {
        return consumerMapper.allUser();
    }
//
//    @Override
//    public List<Consumer> userOfId(Integer id) {
//        return consumerMapper.userOfId(id);
//    }
    @Override
    public List<Consumer> userOfName(String username){ return consumerMapper.userOfName(username);}
//
//    @Override
//    public List<Consumer> loginStatus(String username) {
//        return consumerMapper.loginStatus(username);
//    }


}
